package LockSupport与线程中断.interrupt;

import java.util.concurrent.TimeUnit;

/**
 * @program: algorithm
 * @description: 通过volatile停止一个中断的线程
 * @author: 陈恩涛
 * @create: 2022-08-12 07:26
 **/
public class InterruptDemo1 {
    static volatile boolean isStop = false;

    public static void main(String[] args) {

        final Thread t1 = new Thread(() -> {
            while (true) {
                if (isStop) {
                    System.out.println(Thread.currentThread().getName() + "\t isStop被修改为true,线程终止！");
                    break;
                }
                System.out.println("t1---------hello volatile");
            }
        }, "t1");
        t1.start();

        try {
            TimeUnit.MILLISECONDS.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(() -> {
            isStop = true;
        },"t2").start();
    }
}
